import { SchemaManageAPI } from '../type';

export class Search {
  switchList: SchemaManageAPI.ISwitchList[] = []; //头部切换列表
  searchList: SchemaManageAPI.ISearchList[] = []; //搜索表单列表
  actionList: SchemaManageAPI.IActionBtnList[] = []; //操作按钮列表

  onPropsSearch: any = () => { };
  onPropsSwitch: any = () => { };
  onPropsReset: any = () => { };

  params: Record<string, any> = {};
  searchValues: Record<string, any> = {};
  filterDrawerValues: Record<string, any> = {};
  searchDeptUserValues: Record<string, any> = {};

  constructor() { }

  onSearch(values: any) {
    if (values) {
      this.saveSearchValue(values);
      if (Object.keys(this.filterDrawerValues).length > 0) {
        //高级筛选里有数据时，搜索时带上选中的数据
        this.onPropsSearch({
          ...values,
          ...this.filterDrawerValues,
        });
      } else {
        //无数据，不带
        this.onPropsSearch({
          ...values,
          ...this.searchDeptUserValues,
        });
      }
    }
  }

  onSwitch(key: string | undefined | number, searchValues: any, params: any) {
    this.params = params;
    
    this.onPropsSwitch(key, searchValues, params);
  }

  onReset(values: any) {
    if (values) {
      this.searchValues = {};
      this.filterDrawerValues = {};
      this.searchDeptUserValues = {}
      this.onPropsReset(values);
    }
  }

  /*存储搜索字段 */
  saveSearchValue(values: any) {
    this.searchValues = {
      ...values,
    };
  }

  /**存储组织岗位搜索字段 */
  saveSearchDeptUserValue(values: any) {
    this.searchDeptUserValues = {
      ...values
    }
  }

  /* 存储抽屉搜索字段数据 */
  saveFilterValues(values: any) {
    this.filterDrawerValues = {
      ...values,
    };
  }

  /* 初始化 */
  init(searchProps: SchemaManageAPI.ISearchPropsType) {
    if (searchProps) {
      if (searchProps?.searchList) {
        this.searchList = searchProps.searchList;
      }
      if (searchProps?.switchList) {
        this.switchList = searchProps?.switchList;
      }
      if (searchProps?.onSearch) {
        this.onPropsSearch = searchProps?.onSearch;
      }
      if (searchProps?.onSwitch) {
        this.onPropsSwitch = searchProps?.onSwitch;
      }
      if (searchProps?.onReset) {
        this.onPropsReset = searchProps?.onReset;
      }
    }
  }

  mount(props: SchemaManageAPI.ISearchPropsType) {
    this.init(props);
  }
}
